package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.dto.TagProducer;
import com.ttg.common.entities.BasePage;
import com.ttg.common.utils.DateTimeUtil;
import com.ttg.model.dto.MctInfoListGetDto;
import com.ttg.model.dto.open.QueryVipGradeByMctNoDto;
import com.ttg.model.mapper.BankPublicPrimitiveMapper;
import com.ttg.model.pojo.*;
import com.ttg.model.vo.MctInfoListGetVo;
import com.ttg.model.vo.VipInfoGetListVo;
import com.ttg.model.vo.open.QueryVipGradeByMctVo;
import com.ttg.model.vo.open.QueryVipPublicTagInfoVo;
import com.ttg.web.dao.BankPublicPrimitiveDao;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.time.LocalDate;
import java.util.HashMap;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangHongYuan
 * @description
 * @date 2024/4/11 16:43
 */
@Service
public class BankPublicPrimitiveDaoImpl extends ServiceImpl<BankPublicPrimitiveMapper, BankPublicPrimitive> implements BankPublicPrimitiveDao {
    @Override
    public BankPublicPrimitive getByMctNo(String mctNo) {
        LambdaQueryWrapper<BankPublicPrimitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankPublicPrimitive::getMctNo,mctNo);
        return getOne(queryWrapper);
    }

    @Override
    public QueryVipPublicTagInfoVo queryVipTagByPublic(String mctNo, Integer type) {
        return baseMapper.queryVipTagByPublic(mctNo,type);
    }

    @Override
    public BankPublicPrimitive getByMctNoAndDateLast(String mctNo) {
        LambdaQueryWrapper<BankPublicPrimitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankPublicPrimitive::getMctNo,mctNo);
        queryWrapper.orderByDesc(BankPublicPrimitive::getUpdateTime);
        queryWrapper.last("LIMIT 1");
        return getOne(queryWrapper);
    }

    @Override
    public BasePage<MctInfoListGetVo> getList(BasePage<VipInfoGetListVo> page, MctInfoListGetDto dto) {
        String startDate =null;
        if(Objects.nonNull(dto.getStartTime())){
            startDate = DateTimeUtil.formatYYYY_MM_DD(dto.getStartTime());
        }

        String endtDate =null;
        if(Objects.nonNull(dto.getEndTime())){
            endtDate = DateTimeUtil.formatYYYY_MM_DD(dto.getEndTime());
        }
        return baseMapper.getList(page,dto.getMctId(), dto.getMctNo(), dto.getMctName(), dto.getMctGrade(),dto.getMctStatus(),startDate,endtDate);
    }

    @Override
    public BasePage<BankPublicPrimitive> getAllByPage(BasePage page) {
        return page(page);
    }

    @Override
    public BankPublicPrimitive getByMctNoSelectId(String mctNo) {
        LambdaQueryWrapper<BankPublicPrimitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BankPublicPrimitive::getMctNo);
        queryWrapper.eq(BankPublicPrimitive::getMctNo,mctNo);
        return getOne(queryWrapper);
    }




    @Override
    public Map<String, Long> existsMapByMctNo(List<String> existsWhere) {
        LambdaQueryWrapper<BankPublicPrimitive> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(BankPublicPrimitive::getMctNo, existsWhere);
        Map<String, Long> rsMap = new HashMap<>();
        for (BankPublicPrimitive bankPublicPrimitive : list(lambdaQueryWrapper)) {
            rsMap.put(bankPublicPrimitive.getMctNo(), bankPublicPrimitive.getId());
        }
        return rsMap;
    }


    @Override
    public BasePage<BankPublicPrimitive> getPrimitiveBasesByVipRatingConfig(VipRatingConfig vipRatingConfig, BasePage page) {
        LambdaQueryWrapper<BankPublicPrimitive> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper
                .or(or -> or.ge(BankPublicPrimitive::getMctPractiseYears, vipRatingConfig.getRatingStart())
                        .lt(BankPublicPrimitive::getMctPractiseYears, vipRatingConfig.getRatingEnd())
                )
                .or(or -> or.apply("0={0} ", vipRatingConfig.getRatingEnd())
                                .eq(BankPublicPrimitive::getMctPractiseYears, vipRatingConfig.getRatingStart())
                );
        return page(page, queryWrapper);
    }


    @Override
    public List<String> getMctNosByTags(List<TagProducer> tags, String key) {
        LambdaQueryWrapper<BankPublicPrimitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BankPublicPrimitive::getMctNo);
        for (TagProducer tag: tags) {
            wrapper.apply(key+tag.getCompareStr()+"{0}",tag.getValue());
        }
        List<BankPublicPrimitive> list = list(wrapper);
        if (Objects.isNull(list) || CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(BankPublicPrimitive::getMctNo).collect(Collectors.toList());
    }

//    @Override
//    public void updateGrowByTaskIdAndDate(Long taskId, Integer nowDate, LocalDateTime nowTime) {
//        baseMapper.updateGrowByTaskIdAndDate(taskId,nowDate,nowTime);
//    }

    @Override
    public List<QueryVipGradeByMctVo> queryVipGradeByMctNo(List<QueryVipGradeByMctNoDto> mctNos, Integer typePublic) {
        return baseMapper.queryVipGradeByMctNo(mctNos,typePublic);
    }


    @Override
    public void sumTodayIntegral(Integer excuteDate) {
        baseMapper.sumTodayIntegral(excuteDate);
    }

    @Override
    public List<QueryVipPublicTagInfoVo> queryVipTagByMcyNo(List<String> nos,Integer type) {
        return baseMapper.queryVipTagByMcyNo(nos,type);
    }

    @Override
    public BankPublicPrimitive getLastMctId() {
        LambdaQueryWrapper<BankPublicPrimitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(BankPublicPrimitive::getId)
                .last("limit 1");
        return getOne(wrapper);
    }

    @Override
    public void rattingGradeByBatch(Long idStart, Long idEnd) {
        baseMapper.rattingGradeByBatch(idStart,idEnd);
    }

    @Override
    public boolean newMctRating() {
        return baseMapper.newMctRating();
    }

    @Override
    public List<BankPublicPrimitive> bankPublicPrimitive(int pageSize, long starId) {
        return baseMapper.bankPublicPrimitive(pageSize,starId);
    }
}
